package com.jyu.apts.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	
	private static ThreadLocal<SimpleDateFormat> YYYY_MM_DD_HH_MM_SS_FORMAT_THREADLOCAL = new ThreadLocal<SimpleDateFormat>(){

		@Override
		protected SimpleDateFormat initialValue() {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		}
		
	};
	
	private static ThreadLocal<SimpleDateFormat> YYYY_MM_DD_FORMAT_THREADLOCAL = new ThreadLocal<SimpleDateFormat>(){

		@Override
		protected SimpleDateFormat initialValue() {
			return new SimpleDateFormat("yyyy-MM-dd");
		}
		
	};
	
	private static ThreadLocal<SimpleDateFormat> HH_MM_SS_FORMAT_THREADLOCAL = new ThreadLocal<SimpleDateFormat>(){

		@Override
		protected SimpleDateFormat initialValue() {
			return new SimpleDateFormat("HH:mm:ss");
		}
		
	};
	
	public static SimpleDateFormat getYYYY_MM_DD_Format() {
		return YYYY_MM_DD_FORMAT_THREADLOCAL.get();
	}
	
	public static SimpleDateFormat getHH_MM_SS_Format() {
		return HH_MM_SS_FORMAT_THREADLOCAL.get();
	}
	
	public static SimpleDateFormat getYYYY_MM_DD_HH_MM_SS_Format() {
		return YYYY_MM_DD_HH_MM_SS_FORMAT_THREADLOCAL.get();
	}

	/**
	 * 对时间进行日期单位的运算
	 * @param date要运算的Date对象
	 * @param days正数---加，负数---减
	 * @return 运算后的Date对象
	 */
	public static Date addDays(Date date, int days) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, days);
		return calendar.getTime();
	}
	
	/**
	 * 对时间进行小时单位的运算
	 * @param date要运算的Date对象
	 * @param hours正数---加，负数---减
	 * @return 运算后的Date对象
	 */
	public static Date addHours(Date date, int hours) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.HOUR_OF_DAY, hours);
		return calendar.getTime();
	}
	
	/**
	 * 对时间进行分钟单位的运算
	 * @param date要运算的Date对象
	 * @param mins正数---加，负数---减
	 * @return 运算后的Date对象
	 */
	public static Date addMins(Date date, int mins) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MINUTE, mins);
		return calendar.getTime();
	}
	
	/**
	 * 对时间进行秒单位的运算
	 * @param date要运算的Date对象
	 * @param seconds正数---加，负数---减
	 * @return 运算后的Date对象
	 */
	public static Date addSeconds(Date date, int seconds) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.SECOND,seconds);
		return calendar.getTime();
	}

}
